﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using NewsMine.Logging;

namespace WinApp.JobEngine.Hosting
{
    static class Program
    {
        static INewsMineLogger _logger = NewsMineLogManager.GetLogger("CrawlerApp");

        static String _mutexID = "a8b65a4f-9ffb-46fd-a432-bdd3338c423e";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {


            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);



            Thread.Sleep(2000);

            _mutexID = System.Configuration.ConfigurationManager.AppSettings.Get("mutexid");

            _logger.Info("Starting the app");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            Boolean _isNotRunning;
            Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning);
            if (_isNotRunning)
            {
                Application.Run(new Form1());
                //Application.Run(new frmMigration());
                
            }
            else
            {
                _logger.Info("An application is already running, so that i am not starting");
                //MessageBox.Show("An instance is already running.");
                return;
            }



          


        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {

            _logger.FatalException("Currunt domain unhandled exception is thrown by the crawler winodws app." + Environment.NewLine + (e.ExceptionObject as Exception).Message, e.ExceptionObject as Exception);

            if (e.IsTerminating == true)
            {
                _logger.Fatal("Due to the unhandled exception the application is shutting down.");
            }
            else
            {
                _logger.Fatal("Unhandled exception is captured and the applicatoin safely running.");
            }
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            _logger.FatalException("(Thread Exception) Unhanled exception is thrown by the crawler windows app" + Environment.NewLine + e.Exception.Message, e.Exception);
        }



        public static void MakeSureWeHaveOnlyOneInstanceOfApplication()
        {

            Boolean _isNotRunning=false;

            using (Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning))
            {
                if (_isNotRunning)
                {
                    return;
                }
                else
                {
                    MessageBox.Show("already running");
                    Application.Exit();
                }
            }

        }


    }





   
}
